package com.huanwang.commonlibrary.base;

import com.huanwang.commonlibrary.networkframework.RetrofitClient;
import com.huanwang.commonlibrary.networkframework.RetrofitInterface;
import com.huanwang.commonlibrary.networkframework.consumer.OnResponseListenImpl;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.disposables.CompositeDisposable;
import io.reactivex.rxjava3.schedulers.Schedulers;


public abstract class BaseModuleRepository  {

    private CompositeDisposable mCompositeDisposable;
    public  RetrofitInterface flowable;

    public BaseModuleRepository(){
        flowable = RetrofitClient.INSTANCE.getInstance().create(RetrofitInterface.class);       //retrofit初始化
        mCompositeDisposable = new CompositeDisposable();
    }

    public void addSubscription(Flowable observable, final OnResponseListenImpl consumer) {
        mCompositeDisposable.add(observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(consumer, consumer));
    }


    public void unSubscribe() {
        if (mCompositeDisposable != null) {
            mCompositeDisposable.dispose();
        }
        if (flowable!=null)
            flowable=null;
    }
}
